﻿using System.Security.Principal;
using System.Web;

namespace TestSystem.BLL
{
    public class Utils
    {
        public Utils() { }

        public static string GalleryFolderPath
        {
            get
            {
                string path = HttpContext.Current.Server.MapPath("./Images/Gallery");
                int idx = path.IndexOf("\\Admin");
                path = path.Substring(0, idx) +
                    path.Substring(idx + 6, (path.Length - 6 - idx));
                return path;
            }
        }

        public static bool IsAdmin
        {
            get
            {
                return HttpContext.Current.User.IsInRole("Admin");
            }
        }

        public static bool IsModer
        {
            get
            {
                return HttpContext.Current.User.IsInRole("Moder");
            }
        }

        public static bool IsLoggedIn
        {
            get
            {
                return HttpContext.Current.User != null && HttpContext.Current.User.Identity != null &&
                       HttpContext.Current.User.Identity.IsAuthenticated;
            }
        }

        public static string BoolToYesNo(bool yesno)
        {
            return yesno ? "так" : "ні";
        }

        public static IPrincipal CurrentUser
        {
            get
            {
                return HttpContext.Current.User;
            }
        }

        public static string CurrentUserName
        {
            get
            {
                string userName = "";
                if (HttpContext.Current.User.Identity.IsAuthenticated)
                    userName = HttpContext.Current.User.Identity.Name;
                return userName;
            }
        }

        public static string CurrentUserIP
        {
            get
            {
                return HttpContext.Current.Request.UserHostAddress;
            }
        }
    }
}